#include <iostream>
#include <boost/filesystem/path.hpp>
#include <string>
#include <fstream>
#include <stdio.h>
#include <math.h>
#include "components/CmdLine/cmdargs.h"
#include "components/greta/regexpr2.h"
#include "get_arg.h"
using std::string;
using std::ofstream;
using std::ifstream;
using std::fstream;
using std::cerr;
using std::cout;




//_______________________________________________________________________________________

//		process_command_line_options

//				from command line
//				Created [9/13/2001]

//_______________________________________________________________________________________
bool process_command_line_options(int argc, char* argv[], t_program_args& args)
{
	// optional help directive
	CmdArgUsage opt_help('h', "help", 	"display this message", &cout);

	// required options
	const unsigned REQUIRED = CmdArg::isREQ | CmdArg::isVALREQ;
	CmdArgStr opt_login		('l', "login", "<string>",
								"database login string. "
								"E.g. dbname=any_panda_db "
								"[user=any_user host=any_host password=secret_word]",
								REQUIRED);
	CmdArgStr opt_taxon1	('A', "A_species", "<string>",
								"species A in a set of ortholog pairs. "
								"E.g. human",
								REQUIRED);
	CmdArgStr opt_taxon2	('B', "B_species", "<string>",
								"species B in a set of ortholog pairs. "
								"E.g. mouse",
								REQUIRED);
	CmdArgStr opt_errors	('e', "errors_file", "<string>",
								"file listing errors. "
								"E.g. orthologs.mouse_rat.errors",
								REQUIRED);
	CmdArgSet opt_verbose	('v', "verbose",
								"Display results in human readable form");
	CmdArgSet opt_nodots		('n', "no_dots",
								"Do not print progress dots");
	CmdArgStr opt_file		("[data_file]", "Data file containing gene ids and regions");
	
	string exe_file_name =  boost::filesystem::path(*argv).leaf();
	
	// construct command line
	CmdLine  cmdline(exe_file_name.c_str(),
							&opt_login,
							&opt_taxon1,
							&opt_taxon2,
							&opt_verbose,
							&opt_errors,
							&opt_file,
							&opt_nodots,
							0);
	cmdline.description(
		"This programme calculates the KaKs, percentage identity and bitscores "
		"for ortholog sequences with specified gene identifier pairs.\n\n"
		
		"A tab delimited file needs to be supplied from STDIN or from 'data_file', containing "
			"the gene_ids of each pair of orthologs, "
			"the masked region (e.g. '0:2|5:7' indicates residues 0->2,5->7 inclusive), "
			"and an optional description of the sequence\n\n"
		"The resulting output is sent to STDOUT");
	cmdline.set(CmdLine::NO_ABORT | CmdLine::QUIET);
	cmdline.set(CmdLine::NO_ABORT | CmdLine::QUIET);

	// parse command line
	CmdArgvIter  argv_iter(--argc, ++argv);

	// report command line errors
	if (cmdline.parse(argv_iter))
	{
		// no errors reported initially because quiet is on.
		// This allows us to print exclamation marks here.
		// Rather a lot of work for little gain :->
		cerr << "\n" << string(78, '!')	<< "\n";
		cerr << '!'	<< string(26, ' ')	<< "Parsing errors occurred!"
					<< string(26, ' ')	<< "!\n\n";

		// turn quiet off and print missing arguments if any
		cmdline.clear(CmdLine::QUIET);
		cmdline.ck_need_val();
		cmdline.missing_args();
		cerr << "\n" << string(78, '!') << "\n\n";
		
		// print what is supposed to be on the command line.
		cmdline.usage(cout);



		return false;
	}


	
	args.verbose = opt_verbose;
	args.nodots =opt_nodots;
	args.data_file = opt_file;
	//____________________________________________________________________________________
	// 		Connect to PostgreSQL
	//____________________________________________________________________________________
	args.login = opt_login;
	args.pg_db.reset(new t_pg_cursor(args.login.c_str(), "panda_cursor"));	//"dbname=test user=postgres"
	if (args.pg_db->ConnectionBad() )
	{
		cerr	<< "\nERROR!! Connection to database ["
				<< args.login << "] failed.\n"
				<< args.pg_db->ErrorMessage() << "\n\n";
		cmdline.usage(cerr);
		return false;
	}
	args.error_file = opt_errors;
	args.taxon1 = opt_taxon1;
	args.taxon2 = opt_taxon2;
	if (args.taxon1 == args.taxon2)
	{
		cerr	<< "\nError!! Species A and B must be two different species!\n\n";
		cmdline.usage(cerr);
		return false;
	}
	if (args.taxon1 > args.taxon2)
		args.taxon1.swap(args.taxon2);
	

	return true;
}






